package ru.spb.osll.myfit.client.widget;

import ru.spb.osll.myfit.common.*;

public class PageElementWidgetFactory {
	private GWTWidgetFactory m_gwtWidgetFactory;

	public PageElementWidgetFactory(GWTWidgetFactory gwtWidgetFactory) {
		m_gwtWidgetFactory = gwtWidgetFactory;
	}

	public PageElementWidget getPageElementWidget(PageElement pageElement) {
		PageElementType type = pageElement.getType();
		switch (type) {
		case INCLUDE_ERROR:
			return new IncludeErrorWidget(m_gwtWidgetFactory, (IncludeError) pageElement);
		case INCLUDE:
			return new IncludeWidget(m_gwtWidgetFactory, (Include) pageElement);
		case TABLE:
			return createTypedTable(m_gwtWidgetFactory, (Table) pageElement);
		case PAGE_ERROR:
			return new PageErrorWidget(m_gwtWidgetFactory, (PageError) pageElement);
		case WIKI_TEXT:
			return new WikiTextWidget(m_gwtWidgetFactory, (WikiText) pageElement);
		case SET_UP:
			return new SetUpWidget(m_gwtWidgetFactory, (SetUp) pageElement);
		case TEAR_DOWN:
			return new SetUpWidget(m_gwtWidgetFactory, (TearDown) pageElement);
		case CONTENT:
			return new ContentWidget(m_gwtWidgetFactory, (Content) pageElement);
		case DEFINE:
			return new DefineWidget(m_gwtWidgetFactory, (Define) pageElement);
		default:
			return null;
		}
	}

	public TableWidget createTypedTable(GWTWidgetFactory gwtWidgetFactory, Table table) {
		TableWidget tableWidget;

		switch (table.getStyle()) {
		case DO_FIXTURE:
			tableWidget = new FlexTableWidget(gwtWidgetFactory, table);
			break;

		case SETUP_FIXTURE:
			tableWidget = new FixedTableWidget(gwtWidgetFactory, table);
			break;

		case COLUMN_FIXTURE:
			tableWidget = new FixedTableWidget(gwtWidgetFactory, table);
			break;

		case ONE_LINE_FIXTIRE:
			tableWidget = new OneLineTableWidget(gwtWidgetFactory, table);
			break;

		case MYFIT_TABLE_FIXTURE:
			tableWidget = new MyFitTableWidget(gwtWidgetFactory, table);
			break;

		default:
			// for demo
			tableWidget = new MyFitTableWidget(gwtWidgetFactory, table);
		}

		return tableWidget;
	}
}
